/*!
 * jQuery BOPS Plugin
 *
 * Copyright 2013, zhi-qingchun@qq.com
 * 
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/GPL-2.0
 */
 (function($) {
	 $.fn.er = function(text) {
		 this.hint("ERROR", text);
	 };
	 
	 $.fn.ok = function(text) {
		 this.hint("SUCCESS", text);
	 };
	 
	 $.fn.hint = function(type, text) {
		 var elMark = this.attr("id") ? this.attr("id") : this.attr("name"); 
		 if (! elMark) return;
		 var hintEl = $("#" + elMark + "Hint");
		 if (hintEl.length == 0) {
			hintEl = $("<span>").attr("id", elMark + "Hint");
			hintEl.insertAfter(this);
		} else {
			// do nothing
		}
		hintEl.text(text);
		if (type == "ERROR") {
			hintEl.css("color", "red");
		} else if (type == "SUCCESS") {
			hintEl.css("color", "green");
		}
		// 与相邻元素保持距离
		hintEl.css("margin", "5px");
	 };
	 
	 $.fn.wipe = function() {
		 var elMark = this.attr("id") ? this.attr("id") : this.attr("name"); 
		 if (! elMark) return;
		 var hintEl = $("#" + elMark + "Hint");
		 if (hintEl.length > 0) {
			hintEl.remove();
		}
	 }
	 
	 // XXX 表单元素不能为（目前不能用，有错）
	 $.fn.nn = function() {
		 if (this.val() == "") {
			 this.hint("ERROR", "不能为空");
			 return false;
		 } else {
			 this.wipe();
			 return true;
		 }
	 }
 
	 // 调试工具
	 $.log = function(msg) {
		 console.log(msg);
	 }
 })(jQuery);